package com.itheima.reggie.controller;

import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车管理
 */
@RestController
@RequestMapping("shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查询当前用户的购物车
     * @return 查询结果
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list(){
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        log.info("查询用户:{} 的购物车", userId);
        //查询当前用户的购物车
        List<ShoppingCart> shoppingCarts = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, userId)
                .orderByAsc(ShoppingCart::getCreateTime)
                .list();
        return R.success(shoppingCarts);
    }

    /**
     * 添加商品到购物车
     * @param shoppingCart 商品信息
     * @return 返回结果
     */
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart){
        log.info("添加购物车:{}", shoppingCart);
        //获取用户id ,指定当前用户的购物车
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //查询购物车中是否有 该商品
        ShoppingCart shoppingCartOne = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, shoppingCart.getUserId()) //获取当前用户的购物车
                // 菜品id 存在
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                // 套餐id 存在
                .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .one();
        if (shoppingCartOne == null){
            // 购物车中没有该商品, 添加商品到购物车, 数量为一
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            shoppingCartOne = shoppingCart;
        } else {
            // 购物车中有该商品, 商品数量 加一
            shoppingCartOne.setNumber( shoppingCartOne.getNumber() + 1);
            shoppingCartService.updateById(shoppingCartOne);
        }
        return R.success(shoppingCartOne);
    }

    /**
     * 修改购物车 商品数量减一
     * @param shoppingCart 商品id
     * @return 返回结果
     */
    @PostMapping("/sub")
    public R<String> subtractById(@RequestBody ShoppingCart shoppingCart){
        //获取当前用户
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        log.info("修改购物车商品{} 减一", shoppingCart);
        // 修改商品数量
        shoppingCartService.subtractById(shoppingCart);
        
        return R.success("修改成功");
    }

    /**
     * 清空当前用户购物车
     * @return 返回结果
     */
    @DeleteMapping("/clean")
    public R<String> cleanShoppingCartById(){
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        log.info("清空用户{} 的购物车", userId);
        //清空当前用户 购物车的数据
        boolean remove = shoppingCartService.lambdaUpdate()
                .eq(ShoppingCart::getUserId, userId)
                .remove();
        if (remove){
            return R.success("清空成功");
        } else {
            return R.error("清空失败");
        }
    }
}
